<template>
  <div class="main-container">
    <div class="main-content">
      <div class="row">
        <div class="col-sm-10 col-sm-offset-1">
          <div class="login-container">
            <div class="center">
              <h1>
                <i class="ace-icon fa fa-leaf green"></i>
                <span class="">控台登录</span>
              </h1>
            </div>

            <div class="space-6"></div>

            <div class="position-relative">
              <div id="login-box" class="login-box visible widget-box no-border">
                <div class="widget-body">
                  <div class="widget-main">
                    <h4 class="header blue lighter bigger">
                      <i class="ace-icon fa fa-coffee green"></i>
                      请输入用户名密码
                    </h4>

                    <div class="space-6"></div>

                    <form>
                      <fieldset>
                        <label class="block clearfix">
                            <span class="block input-icon input-icon-right">
															<input type="text"
                                     class="form-control"
                                     v-model="user.loginName"
                                     placeholder="登录名test"/>
															<i class="ace-icon fa fa-user"></i>
														</span>
                        </label>

                        <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password"
                                     class="form-control"
                                     v-model="user.password"
                                     placeholder="登录密码123"/>
															<i class="ace-icon fa fa-lock"></i>
														</span>
                        </label>

                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <div class="input-group">
                              <input v-model="user.imageCode" type="text" class="form-control" placeholder="验证码">
                              <span class="input-group-addon" id="basic-addon2">
                                <img v-on:click="loadImageCode()" id="image-code" alt="验证码"/>
                              </span>
                            </div>
                          </span>
                        </label>

                        <div class="space"></div>

                        <div class="clearfix">
                          <label class="inline">
                            <input v-model="remember" type="checkbox" class="ace"/>
                            <span class="lbl"> 记住我</span>
                          </label>

                          <button type="button"
                                  class="width-35 pull-right btn btn-sm btn-primary"
                                  @click="login()">
                            <i class="ace-icon fa fa-key"></i>
                            <span class="bigger-110">登录</span>
                          </button>
                        </div>

                        <div class="space-4"></div>
                      </fieldset>
                    </form>
                  </div><!-- /.widget-main -->

                </div><!-- /.widget-body -->
              </div><!-- /.login-box -->

            </div><!-- /.position-relative -->

          </div>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.main-content -->
  </div><!-- /.main-container -->
</template>

<script>
  export default {
    name: 'login',
    data() {
      return {
        user: {
          loginName: '',
          password: '',
          imageCode: '',
          imageCodeToken: ''
        },
        users: [],
        remember: true,
        imageCodeToken: ''
      }
    },
    mounted() {
      let _this = this
      $('body').removeClass('no-skin').attr('class', 'login-layout light-login')

      if (_this.$route.query.status === '401') {
        Toast.error('尚未登录，请登录！')
      }

      Loading.hide()

      // 从缓存中获取记住的用户名密码，如果获取不到，说明上一次没有勾选“记住我”
      let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER)
      if (rememberUser) {
        _this.user = rememberUser
      }

      _this.loadImageCode()
    },
    methods: {
      login () {
        let _this = this
        //  如果是第一次登录, 那么走正常流程, 输入的密码被加盐加密一次给后端
        //  如果勾选了记住我, 第二次登录, 这个_this.user则是从缓存中取出的, 密码已经是加盐加密过后的了, 则密码不需要再加盐加密, 直接传给后端
        let md5 = hex_md5(_this.user.password)
        let rememberUser = LocalStorage.get(LOCAL_KEY_REMEMBER_USER) || {}
        if (md5 !== rememberUser.md5) {
          //  密码加密(MD5)
          _this.user.password = hex_md5(_this.user.password + KEY)
        }

        _this.user.imageCodeToken = _this.imageCodeToken;

        if (1 != 1
          || !Validator.require(_this.user.loginName, "用户名")
          || !Validator.require(_this.user.password, "密码")
          || !Validator.require(_this.user.imageCode, "验证码")
        ) {
          return;
        }

        Loading.show()
        _this.$axios.post(process.env.VUE_APP_SERVER + '/system/admin/user/login', _this.user).then((resp) => {
          if (resp.data.success) {
            //  登录成功
            Loading.hide()
             let loginUser = resp.data.content
            //  保存用户信息到SessionStorage
            Tool.setLoginUser(loginUser)
            //  是否勾选'记住我'功能
            if (_this.remember) {
              let md5 = hex_md5(_this.user.password)  // 将经过一次加盐加密的密码再加密一次
              LocalStorage.set(LOCAL_KEY_REMEMBER_USER, {
                loginName: loginUser.loginName,
                password: _this.user.password,  //  保存的经过一次加密的密码
                md5: md5
              })
            } else  {
              LocalStorage.set(LOCAL_KEY_REMEMBER_USER, null)
            }
            //  跳转到welcome
            this.$router.push('/welcome')
          } else {
            Loading.hide()

            Toast.warning(resp.data.message)
            _this.user.password = ''
            _this.loadImageCode()
          }
        })
      },

      loadImageCode() {
        let _this = this
        _this.imageCodeToken = Tool.uuid(8)
        $('#image-code').attr('src', process.env.VUE_APP_SERVER + '/system/admin/kaptcha/image-code/' + _this.imageCodeToken)
      }
    }
  }
</script>

<style>
  .input-group-addon {
    padding: 0;
  }
</style>
